package com.beordie.filter;

import com.beordie.model.Admin;
import com.beordie.utils.AdminUtils;
import com.beordie.utils.UserUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname PassportFilter
 * @Description 通行证
 * @Date 2021/6/29 19:43
 * @Created 30500
 */
public class PassportFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * @description 身份验证拦截页面信息
     * @author 30500
     * @date 2021/6/30 9:47
     * @type [javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain]
     * @return void
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("拦截到请求");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 获取需要进行登录的页面请求信息, 进行鉴权处理
        String uri = request.getRequestURI();
        if (uri.endsWith("/admin/login.html") ||
                uri.endsWith("/admin/login.udo") ||
                uri.endsWith("/login.html") ||
                uri.endsWith("/wx/loginP.udo") ||
                uri.endsWith("/wx/login.udo"))
            filterChain.doFilter(request, response);
        else if (uri.indexOf("admin") != -1 && AdminUtils.getAdminName(request.getSession()) != null)
            filterChain.doFilter(request, response);
        else if (uri.indexOf("admin") != -1)
            response.sendRedirect("/admin/login.html");
        else if (uri.endsWith(".html") && UserUtils.getUserInfo(request.getSession()) == null)
            response.sendRedirect("/login.html");
        else
            filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
